import {
  Shader,
  ShaderData,
  ShaderProperty,
  ShaderPool,
  ShaderProgram,
  ShaderProgramPool,
  ShaderMacro,
  ShaderMacroCollection,
  ShaderDataGroup,
  ShaderLib
} from "@nirvana/shaders";
import { Color, Vector3 } from "@nirvana/math";

import { WebGLCanvas } from "@nirvana/canvas";
import { Data, Mesh, DataElement, Geometry, SimpleMaterial, Camera } from "@nirvana/core";

// 使用使用 Canvas
let canvas = document.getElementById("engine-nirvana");
const webCanvas = new WebGLCanvas(canvas, {});
const engine = webCanvas.engine;
const gl = engine.gl;

const pData = new Float32Array([
  -0.5, -0.5, -0.5, 0.5,-0.5,-0.5, 0.5, 0.5,-0.5, -0.5, 0.5,-0.5,
  -0.5,-0.5, 0.5, 0.5,-0.5, 0.5, 0.5, 0.5, 0.5, -0.5, 0.5, 0.5,
  -0.5,-0.5,-0.5, -0.5, 0.5,-0.5, -0.5, 0.5, 0.5, -0.5,-0.5, 0.5,
  0.5,-0.5,-0.5, 0.5, 0.5,-0.5, 0.5, 0.5, 0.5, 0.5,-0.5, 0.5,
  -0.5,-0.5,-0.5, -0.5,-0.5, 0.5, 0.5,-0.5, 0.5, 0.5,-0.5,-0.5,
  -0.5, 0.5,-0.5, -0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,-0.5
]);
const cData = new Float32Array([
  5,3,7, 5,3,7, 5,3,7, 5,3,7,
  1,1,3, 1,1,3, 1,1,3, 1,1,3,
  0,0,1, 0,0,1, 0,0,1, 0,0,1,
  1,0,0, 1,0,0, 1,0,0, 1,0,0,
  1,1,0, 1,1,0, 1,1,0, 1,1,0,
  0,1,0, 0,1,0, 0,1,0, 0,1,0
]);
const iData = new Uint16Array([
  0,1,2, 0,2,3, 4,5,6, 4,6,7,
  8,9,10, 8,10,11, 12,13,14, 12,14,15,
  16,17,18, 16,18,19, 20,21,22, 20,22,23
]);


const gem = new Geometry();
gem.addData(new Data(pData, [new DataElement("POSITION", 3)]));
gem.addData(new Data(cData, [new DataElement("COLOR", 3, gl.FLOAT, false, 0, 0)]));
gem.addData(new Data(iData));

const entity = new Mesh(gem, new SimpleMaterial("", { color: true }));
const entity1 = new Mesh(gem, new SimpleMaterial("", { color: true }));
const entity2 = new Mesh(gem, new SimpleMaterial("", { color: true }));
const entity3 = new Mesh(gem, new SimpleMaterial("", { color: true }));

const scene = engine.sceneManager.createScene("root");
scene.addEntity(entity);
scene.addEntity(entity1);
scene.addEntity(entity2);
scene.addEntity(entity3);
const camera = new Camera(engine);
scene.addCamera(camera);
camera.transform.setPosition(new Vector3(0, 0, 10));

// entity.transform.rotate(new Vector3(0, 90, 0));

entity1.transform.translate(new Vector3(-2, 2, 0));
entity2.transform.translate(new Vector3(2, 2, 0));
entity3.transform.translate(new Vector3(0, -2, 0));
//
// entity.add(entity1);
// entity.add(entity2);
// entity.add(entity3);
// entity1.transform.scale(new Vector3(0.8, 0.8, 0.8));
// entity2.transform.scale(new Vector3(0.5, 0.5, 0.5));
// entity3.transform.scale(new Vector3(0.4, 0.4, 0.4));


engine.update = () => {
  // // console.log('--');
  // gl.clearColor(0.0, 0.0, 0.0, 1.0);
  // // 用上面指定的颜色清除缓冲区
  // gl.clear(gl.COLOR_BUFFER_BIT);
  // entity.transform.translate(new Vector3(0, 0.01, 0));
  // entity1.transform.translate(new Vector3(0.01, 0.02, 0));
  // entity2.transform.translate(new Vector3(-0.01, 0.02, 0));
  // entity3.transform.translate(new Vector3(0.01, -0.02, 0));
  // let rand = 5 * Math.random();
  // entity.transform.rotate(new Vector3(0, 0, rand));
  entity.transform.rotate(new Vector3(0, 0, 1));
  // entity.transform.rotate(new Vector3(0, 0, 2));
  // entity2.transform.rotate(new Vector3(0, 0, 4));
  // entity3.transform.rotate(new Vector3(0, 0, 6));
  // entity.transform.scale(new Vector3(rand, rand, rand));
};
